<script lang="ts" name="GoodsInfo" setup>
import useStore from '@/store'
import { watchEffect } from 'vue';
import {useRoute} from 'vue-router'
import {storeToRefs} from 'pinia'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDeatil from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'

import { ref } from 'vue';
import Message from '@/components/message';
const route = useRoute()
const {goods,cart} = useStore()

watchEffect(()=>{
    goods.getGoodsInfo(route.params.id as string)
})

const {goodsInfo} = storeToRefs(goods)


// 获取商品的sku
const currentSkuId = ref('')
const changeSku = (skuId:string) => {
   currentSkuId.value = skuId
   const sku = goodsInfo.value.skus.find((item)=> item.id === skuId)
   if(sku){
     goodsInfo.value.inventory = sku.inventory //更新库存
     goodsInfo.value.price = sku.price  //更新价格
     goodsInfo.value.oldPrice  = sku.oldPrice  //更新老价格
   }
}

// 设置商品sku的数量
const number = ref(1)

// 添加到购物车功能
const addCart = async ()=>{
  // console.log("加入购物车");
  if(currentSkuId.value===''){
    Message.warning("请选择完整商品规格")
    return 
  }

  await cart.addCart(currentSkuId.value,number.value)
  Message.success("添加购物车成功")
}

</script>

<template>
  <div class="seal-goods-page" v-if="goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <SealBread>
        <SealBreadItem to="/">首页</SealBreadItem>
        <SealBreadItem :to="`/category/${goodsInfo.categories[1].id}`">{{ goodsInfo.categories[1].name }}</SealBreadItem>
        <SealBreadItem :to="`/category/sub/${ goodsInfo.categories[0].id}`">{{ goodsInfo.categories[0]?.name }}</SealBreadItem>
        <SealBreadItem to="/">{{ goodsInfo.name }}</SealBreadItem>
      </SealBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
            <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
            <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
            <GoodsName :goods="goodsInfo"></GoodsName>
            <GoodsSku :goods="goodsInfo" @changeSku="changeSku"></GoodsSku>
            <SealNumbox v-model="number"></SealNumbox>
            <SealButton type="primary" style="margin-top:15px;" @click="addCart">加入购物车</SealButton>
        </div>
      </div>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDeatil :goods="goodsInfo"></GoodsDeatil>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
